library(tidyverse)  # data manipulation
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.0.6     ✓ dplyr   1.0.4
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(cluster)    # clustering algorithms
library(factoextra) # clustering algorithms & visualization
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
# Demo data
dataFrame <- data.frame(
    row.names = c("PopRock", "RnB", "Electronics", "AvantGarde", "Blues", "Children", "Classical", "ComedySpoken", "Country", "EasyListening", "Folk", "International", "Jazz", "Latin", "NewAge", "Reggae", "Religious", "StageNScreen", "Vocals"),
      danceability= c(0.51104,
                  0.621307,
                  0.58697,
                  0.55542,
                  0.569593,
                  0.684094,
                  0.336415,
                  0.555449,
                  0.585404,
                  0.447982,
                  0.528089,
                  0.563343,
                  0.516395,
                  0.626992,
                  0.373497,
                  0.704057,
                  0.540279,
                  0.290541,
                  0.457029
                  ),
      energy =    c(0.6468,
                  0.546589,
                  0.63980,
                  0.28162,
                  0.439803,
                  0.432585,
                  0.180278,
                  0.554109,
                  0.495096,
                  0.345165,
                  0.273193,
                  0.433363,
                  0.333980,
                  0.595547,
                  0.206824,
                  0.514850,
                  0.603603,
                  0.235655,
                  0.253629
                  ),
      valence =   c(0.55538,
                  0.615532,
                  0.427237,
                  0.490805,
                  0.652932,
                  0.706491,
                  0.328726,
                  0.471772,
                  0.599310,
                  0.421182,
                  0.517992,
                  0.651033,
                  0.489697,
                  0.690958,
                  0.217313,
                  0.717875,
                  0.465337,
                  0.203338,
                  0.426028
                  ),
      tempo =     c(123.14705,
                  116.758008,
                  119.344773,
                  114.562697,
                  117.491006,
                  121.143491,
                  106.366333,
                  104.896751,
                  119.640495,
                  112.326550,
                  115.630660,
                  116.339237,
                  113.060721,
                  120.644974,
                  110.961211,
                  116.501575,
                  120.617865,
                  103.098356,
                  109.831115
                  ),
      loudness = c(-9.20412,
                  -9.800735,
                  -9.782777,
                  -14.338054,
                  -11.498389,
                  -9.558302,
                  -19.667728,
                  -14.207246,
                  -10.609901,
                  -14.522501,
                  -15.200286,
                  -11.566676,
                  -14.624186,
                  -8.626783,
                  -18.779600,
                  -10.431030,
                  -7.978997,
                  -17.889201,
                  -13.698602
                  ),
      mode =      c(0.73970,
                  0.629490,
                  0.555932,
                  0.619910,
                  0.777985,
                  0.867925,
                  0.758907,
                  0.778816,
                  0.928174,
                  0.696154,
                  0.794603,
                  0.672479,
                  0.580040,
                  0.675282,
                  0.686792,
                  0.647761,
                  0.810370,
                  0.682016,
                  0.710580
                  ),
      key =      c(5.1805658,
                  5.344163,
                  5.455933,
                  5.466063,
                  5.034515,
                  4.377358,
                  4.884334,
                  5.127726,
                  5.342288,
                  4.870513,
                  5.149925,
                  5.122820,
                  4.905347,
                  5.227634,
                  4.513208,
                  5.673134,
                  5.226667,
                  4.834926,
                  5.258562
                  ),
      acousticness = c(0.25420,
                   0.359069,
                   0.231992,
                   0.738263,
                   0.634420,
                   0.660398,
                   0.940046,
                   0.794572,
                   0.469606,
                   0.698889,
                   0.760548,
                   0.701630,
                   0.729896,
                   0.441365,
                   0.854203,
                   0.295487,
                   0.340366,
                   0.774718,
                   0.820661
                  ),
      instrumentalness = c(0.09241,
                   0.036275,
                   0.383143,
                   0.149248,
                   0.084210,
                   0.009765,
                   0.518732,
                   0.003496,
                   0.035471,
                   0.527300,
                   0.071377,
                   0.169907,
                   0.396698,
                   0.044371,
                   0.719714,
                   0.098852,
                   0.013442,
                   0.568359,
                   0.021624
                  ),
      liveness = c(0.21617,
                  0.194166,
                  0.200414,
                  0.173906,
                  0.205921,
                  0.273757,
                  0.194257,
                  0.611077,
                  0.185207,
                  0.168208,
                  0.184784,
                  0.207664,
                  0.186170,
                  0.184466,
                  0.145138,
                  0.198535,
                  0.244273,
                  0.173066,
                  0.211890
                  ),
      speechiness = c(0.05903,
                  0.073552,
                  0.077413,
                  0.059323,
                  0.065374,
                  0.088896,
                  0.065237,
                  0.658424,
                  0.045802,
                  0.045504,
                  0.064941,
                  0.092366,
                  0.055622,
                  0.060981,
                  0.045157,
                  0.133643,
                  0.063291,
                  0.060431,
                  0.058829
                  ),
      explicit = c(0.04573,
                  0.092887,
                  0.076271,
                  0.000000,
                  0.001866,
                  0.000000,
                  0.000000,
                  0.208723,
                  0.003619,
                  0.000000,
                  0.000750,
                  0.025777,
                  0.000664,
                  0.016567,
                  0.000000,
                  0.021642,
                  0.000000,
                  0.000000,
                  0.000000
                  ),
      duration_ms = c(239916.5017,
                  246655.829,
                  284212.461,
                  211989.579186,
                  214966.204291,
                  129214.679245,
                  291495.821864,
                  219118.591900,
                  195926.202533,
                  174263.412821,
                  196734.868066,
                  265348.237301,
                  303131.455829,
                  228668.884029,
                  291237.694340,
                  241206.968657,
                  263544.971852,
                  217302.643788,
                  188957.381146
                  ),
      popularity  = c(40.92764,
                  39.145475,
                  49.548305,
                  23.009050,
                  22.248134,
                  22.698113,
                  12.382138,
                  21.869159,
                  35.641286,
                  18.833333,
                  24.091454,
                  19.598939,
                  20.853703,
                  44.378728,
                  37.286792,
                  36.302239,
                  41.890370,
                  32.215465,
                  20.364869
                  ))
normalize <- function(x) {
return ((x - min(x)) / (max(x) - min(x))) }
dataFrame$tempo <- normalize(dataFrame$tempo)
dataFrame$duration_ms <- normalize(dataFrame$duration_ms)
dataFrame$loudness <- normalize(dataFrame$loudness)
dataFrame$mode <- normalize(dataFrame$mode)
dataFrame$key <- normalize(dataFrame$key)
dataFrame$popularity <- normalize(dataFrame$popularity)
library(fmsb)
max_min <- data.frame(
   danceability = c(1, 0), energy = c(1, 0),
  valence = c(1, 0), tempo = c(1, 0), loudness = c(1, 0),
  mode = c(1, 0), key = c(1, 0), acousticness = c(1, 0), instrumentalness = c(1,0), liveness = c(1,0), speechiness = c(1,0),  explicit = c(1,0), duration_ms = c(1,0), popularity = c(1,0)
)
rownames(max_min) <- c("Max", "Min")

# Bind the variable ranges to the data
df <- rbind(max_min, dataFrame)
df
# Plot the data for Pop/Rock 
library(fmsb)
PopRock<- df[c("Max", "Min", "PopRock"), ]
radarchart(PopRock)

# Plot the data for R&B
library(fmsb)
RnB <- df[c("Max", "Min", "RnB"), ]
radarchart(RnB)

# Plot the data for Electronics 
library(fmsb)
Electronics <- df[c("Max", "Min", "Electronics"), ]
radarchart(Electronics)

# Plot the data for Avant-Garde
library(fmsb)
AvantGarde <- df[c("Max", "Min", "AvantGarde"), ]
radarchart(AvantGarde)

# Plot the data for Blues 
library(fmsb)
Blues <- df[c("Max", "Min", "Blues"), ]
radarchart(Blues)

# Plot the data for Children 
library(fmsb)
Children<- df[c("Max", "Min", "Children"), ]
radarchart(Children)

# Plot the data for Classical 
library(fmsb)
Classical <- df[c("Max", "Min", "Classical"), ]
radarchart(Classical)

# Plot the data for ComedySpoken 
library(fmsb)
ComedySpoken <- df[c("Max", "Min", "ComedySpoken"), ]
radarchart(ComedySpoken)

# Plot the data for Country 
library(fmsb)
Country<- df[c("Max", "Min", "Country"), ]
radarchart(Country)

# Plot the data for Easy Lisenting  
library(fmsb)
EasyListening<- df[c("Max", "Min", "EasyListening"), ]
radarchart(EasyListening)

# Plot the data for Folk   
library(fmsb)
Folk<- df[c("Max", "Min", "Folk"), ]
radarchart(Folk)

# Plot the data for International   
library(fmsb)
International<- df[c("Max", "Min", "International"), ]
radarchart(International)

# Plot the data for Jazz   
library(fmsb)
Jazz<- df[c("Max", "Min", "Jazz"), ]
radarchart(Jazz)

# Plot the data for Latin   
library(fmsb)
Latin<- df[c("Max", "Min", "Latin"), ]
radarchart(Latin)

# Plot the data for New Age   
library(fmsb)
NewAge<- df[c("Max", "Min", "NewAge"), ]
radarchart(NewAge)

# Plot the data for Reggae    
library(fmsb)
Reggae<- df[c("Max", "Min", "Reggae"), ]
radarchart(Reggae)

# Plot the data for Religious    
library(fmsb)
Religious<- df[c("Max", "Min", "Religious"), ]
radarchart(Religious)

# Plot the data for StageNScreen    
library(fmsb)
StageNScreen<- df[c("Max", "Min", "StageNScreen"), ]
radarchart(StageNScreen)

# Plot the data for Vocals    
library(fmsb)
Vocals<- df[c("Max", "Min", "Vocals"), ]
radarchart(Vocals)

create_beautiful_radarchart <- function(data, color = "#00AFBB", 
                                        vlabels = colnames(data), vlcex = 0.7,
                                        caxislabels = NULL, title = NULL, ...){
  radarchart(
    data, axistype = 1,
    # Customize the polygon
    pcol = color, pfcol = scales::alpha(color, 0.5), plwd = 2, plty = 1,
    # Customize the grid
    cglcol = "grey", cglty = 1, cglwd = 0.8,
    # Customize the axis
    axislabcol = "grey", 
    # Variable labels
    vlcex = vlcex, vlabels = vlabels,
    caxislabels = caxislabels, title = title, ...
  )
}
# Reduce plot margin using par()
op <- par(mar = c(1, 2, 2, 2))
# Create the radar charts
create_beautiful_radarchart(
  data = df, caxislabels = c(0, 5, 10, 15, 20),
  color = c("#00AFBB", "#E7B800", "#FC4E07", "#33FF5B", "#9755F7", "#F9F63D", "#3DF9BA", "#F57527")
)
# Add an horizontal legend
legend(
  x = "bottom", legend = rownames(df[-c(1,2),]), horiz = TRUE,
  bty = "n", pch = 20 , col = c("#00AFBB", "#E7B800", "#FC4E07", "#33FF5B", "#9755F7", "#F9F63D", "#3DF9BA", "#F57527"),
  text.col = "black", cex = 0.5, pt.cex = 1.5
  )

par(op)
library(fmsb)
max_min <- data.frame(
   danceability = c(1, 0), energy = c(1, 0),
  valence = c(1, 0), tempo = c(1, 0), loudness = c(1, 0),
  mode = c(1, 0), key = c(1, 0), acousticness = c(1, 0), instrumentalness = c(1,0), liveness = c(1,0), speechiness = c(1,0),  explicit = c(1,0), duration_ms = c(1,0), popularity = c(1,0)
)
rownames(max_min) <- c("Max", "Min")

# Bind the variable ranges to the data
df <- rbind(max_min, dataFrame)
df
dataFrame
df2 <- t(dataFrame) %>%
  as.data.frame() %>%
  rownames_to_column("Field")
df2
library(tidyverse)
library(ggpubr)
ggdotchart(
  df2, x = "Field", y = "PopRock",
  add = "segments", sorting = "descending",
  ylab = "Values", title = "PopRock"
  )

library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
df_scaled <- round(apply(df, 2, scales::rescale), 2)
df_scaled <- as.data.frame(df_scaled)
head(df_scaled)
# Variables summary
# Get the minimum and the max of every column  
col_max <- apply(df_scaled, 2, max)
col_min <- apply(df_scaled, 2, min)
# Calculate the average profile 
col_mean <- apply(df_scaled, 2, mean)
# Put together the summary of columns
col_summary <- t(data.frame(Max = col_max, Min = col_min, Average = col_mean))


# Bind variables summary to the data
df_scaled2 <- as.data.frame(rbind(col_summary, df_scaled))
head(df_scaled2)
opar <- par() 
# Define settings for plotting in a 3x4 grid, with appropriate margins:
par(mar = rep(0.8,4))
par(mfrow = c(2,4))
# Produce a radar-chart for each student
for (i in 6:nrow(df_scaled2)) {
  radarchart(
    df_scaled2[c(1:3, i), ],
    pfcol = c("#99999980",NA),
    pcol= c(NA,2), plty = 1, plwd = 2,
    title = row.names(df_scaled2)[i]
  )
}

# Restore the standard par() settings
par <- par(opar)
## Warning in par(opar): graphical parameter "cin" cannot be set
## Warning in par(opar): graphical parameter "cra" cannot be set
## Warning in par(opar): graphical parameter "csi" cannot be set
## Warning in par(opar): graphical parameter "cxy" cannot be set
## Warning in par(opar): graphical parameter "din" cannot be set
## Warning in par(opar): graphical parameter "page" cannot be set

# create a word cloud
script <- "http://www.sthda.com/upload/rquery_wordcloud.r"
source(script)
res<-rquery.wordcloud("/Users/aniketsingh/Desktop/music/JFKspeech.txt", 
                      type ="file", 
                      lang = "english")
## Loading required package: NLP
## 
## Attaching package: 'NLP'
## The following object is masked from 'package:ggplot2':
## 
##     annotate
## Loading required package: RColorBrewer
## Warning in readLines(x): incomplete final line found on '/Users/aniketsingh/
## Desktop/music/JFKspeech.txt'
## Warning in tm_map.SimpleCorpus(docs, content_transformer(tolower)):
## transformation drops documents
## Warning in tm_map.SimpleCorpus(docs, removeNumbers): transformation drops
## documents
## Warning in tm_map.SimpleCorpus(docs, removeWords, stopwords(lang)):
## transformation drops documents
## Warning in tm_map.SimpleCorpus(docs, removePunctuation): transformation drops
## documents
## Warning in tm_map.SimpleCorpus(docs, stripWhitespace): transformation drops
## documents